cmake_minimum_required(VERSION 2.6.3)

IF(DEFINED CMAKE_BUILD_TYPE)
     SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel")
ELSE(DEFINED CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel")
ENDIF(DEFINED CMAKE_BUILD_TYPE)

PROJECT(all)

IF(NOT DEFINED CRTMPSERVER_SOURCES_ROOT)
	SET(CRTMPSERVER_SOURCES_ROOT "../../sources")
ENDIF(NOT DEFINED CRTMPSERVER_SOURCES_ROOT)
GET_FILENAME_COMPONENT(CRTMPSERVER_SOURCES_ROOT "${CRTMPSERVER_SOURCES_ROOT}" ABSOLUTE)
#MESSAGE("CRTMPSERVER_SOURCES_ROOT: ${CRTMPSERVER_SOURCES_ROOT}")

IF(NOT DEFINED CRTMPSERVER_CONSTANTS_ROOT)
    SET(CRTMPSERVER_CONSTANTS_ROOT "../../constants")
ENDIF(NOT DEFINED CRTMPSERVER_CONSTANTS_ROOT)
GET_FILENAME_COMPONENT(CRTMPSERVER_CONSTANTS_ROOT "${CRTMPSERVER_CONSTANTS_ROOT}" ABSOLUTE)
#MESSAGE("CRTMPSERVER_CONSTANTS_ROOT: ${CRTMPSERVER_CONSTANTS_ROOT}")

IF(NOT DEFINED CRTMPSERVER_3RDPARTY_ROOT)
	SET(CRTMPSERVER_3RDPARTY_ROOT "../../3rdparty")
ENDIF(NOT DEFINED CRTMPSERVER_3RDPARTY_ROOT)
GET_FILENAME_COMPONENT(CRTMPSERVER_3RDPARTY_ROOT "${CRTMPSERVER_3RDPARTY_ROOT}" ABSOLUTE)
#MESSAGE("CRTMPSERVER_3RDPARTY_ROOT: ${CRTMPSERVER_3RDPARTY_ROOT}")

SET(CRTMPSERVER_ROOT "lib/crtmpserver")

IF(DEFINED CRTMPSERVER_INSTALL_PREFIX)
	SET(CMAKE_INSTALL_PREFIX ${CRTMPSERVER_INSTALL_PREFIX})
	SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
	SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
	SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CRTMPSERVER_ROOT}")
	SET(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${CRTMPSERVER_ROOT}")
ELSE(DEFINED CRTMPSERVER_INSTALL_PREFIX)
	SET(CMAKE_INSTALL_PREFIX /tmp/crtmpserver)
ENDIF(DEFINED CRTMPSERVER_INSTALL_PREFIX)

#sample commit to trigger HUDSON

#detect additional libraries
SET(OpenSSL_FIND_REQUIRED true)
SET(CRYPTO_FIND_REQUIRED true)
INCLUDE(cmake_find_modules/Find_openssl.cmake)
INCLUDE(cmake_find_modules/Find_dl.cmake)
INCLUDE(cmake_find_modules/pch_support.cmake)
INCLUDE(CMakeDetermineSystem)
INCLUDE(TestBigEndian)


#setup the endianess if not already defined in toolchain file
if(NOT DEFINED NO_ENDIANESS)
	TEST_BIG_ENDIAN(ENDIANESS)
	IF(ENDIANESS)
		ADD_DEFINITIONS(-DBIG_ENDIAN_BYTE_ALIGNED)
	ELSE(ENDIANESS)
		ADD_DEFINITIONS(-DLITTLE_ENDIAN_BYTE_ALIGNED)
	ENDIF(ENDIANESS)
ENDIF(NOT DEFINED NO_ENDIANESS)


FILE(WRITE ${CRTMPSERVER_SOURCES_ROOT}/common/include/version.h "//THIS FILE IS GENERATED\n")

EXECUTE_PROCESS(COMMAND svnversion -n ${CRTMPSERVER_SOURCES_ROOT} OUTPUT_VARIABLE TEMP_VAL)
FILE(APPEND ${CRTMPSERVER_SOURCES_ROOT}/common/include/version.h "#define CRTMPSERVER_VERSION_BUILD_NUMBER \"${TEMP_VAL}\"\n")

EXECUTE_PROCESS(COMMAND date -u +%s OUTPUT_VARIABLE TEMP_VAL)
FILE(APPEND ${CRTMPSERVER_SOURCES_ROOT}/common/include/version.h "#define CRTMPSERVER_VERSION_BUILD_DATE ${TEMP_VAL}\n")

IF(DEFINED CRTMPSERVER_VERSION_RELEASE_NUMBER)
	FILE(APPEND ${CRTMPSERVER_SOURCES_ROOT}/common/include/version.h "#define CRTMPSERVER_VERSION_RELEASE_NUMBER \"${CRTMPSERVER_VERSION_RELEASE_NUMBER}\"\n")
ENDIF(DEFINED CRTMPSERVER_VERSION_RELEASE_NUMBER)

IF(DEFINED CRTMPSERVER_VERSION_CODE_NAME)
	FILE(APPEND ${CRTMPSERVER_SOURCES_ROOT}/common/include/version.h "#define CRTMPSERVER_VERSION_CODE_NAME \"${CRTMPSERVER_VERSION_CODE_NAME}\"\n")
ENDIF(DEFINED CRTMPSERVER_VERSION_CODE_NAME)

#supported features
ADD_DEFINITIONS(-DHAS_PROTOCOL_HTTP)
ADD_DEFINITIONS(-DHAS_PROTOCOL_RTMP)
ADD_DEFINITIONS(-DHAS_PROTOCOL_LIVEFLV)
ADD_DEFINITIONS(-DHAS_PROTOCOL_RTP)
ADD_DEFINITIONS(-DHAS_PROTOCOL_TS)
ADD_DEFINITIONS(-DHAS_PROTOCOL_VAR)
ADD_DEFINITIONS(-DHAS_PROTOCOL_CLI)
#ADD_DEFINITIONS(-DHAS_PROTOCOL_MMS)
#ADD_DEFINITIONS(-DHAS_PROTOCOL_RAWHTTPSTREAM)
ADD_DEFINITIONS(-DHAS_MEDIA_MP3)
ADD_DEFINITIONS(-DHAS_MEDIA_MP4)
ADD_DEFINITIONS(-DHAS_MEDIA_FLV)
#ADD_DEFINITIONS(-DHAS_MEDIA_NSV)
#ADD_DEFINITIONS(-DHAS_SYSLOG)
ADD_DEFINITIONS(-DGLOBALLY_ACCOUNT_BYTES)
#ADD_DEFINITIONS(-DHAS_VOD_MANAGER)

IF($ENV{COMPILE_STATIC} MATCHES "1")
	MESSAGE(STATUS "Compile static")
	ADD_DEFINITIONS(-DCOMPILE_STATIC)
	SET(LIB_TYPE STATIC)
	SET(LUA_LIBRARY_PATH "lua")
	SET(LUA_INCLUDE_PATH "${CRTMPSERVER_3RDPARTY_ROOT}/lua-dev")
	SET(TINYXML_LIBRARY_PATH "tinyxml")
	SET(TINYXML_INCLUDE_PATH "${CRTMPSERVER_3RDPARTY_ROOT}/tinyxml")
#	SET(TEMP_EXE_LINK_C_FLAGS "${CMAKE_EXE_LINK_STATIC_C_FLAGS}")
#	SET(TEMP_EXE_LINK_CXX_FLAGS "${CMAKE_EXE_LINK_STATIC_CXX_FLAGS}")
#	SET(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS ${TEMP_EXE_LINK_C_FLAGS})
#	SET(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS ${TEMP_EXE_LINK_CXX_FLAGS})
#	SET(CMAKE_EXE_LINK_STATIC_C_FLAGS ${TEMP_EXE_LINK_C_FLAGS})
#	SET(CMAKE_EXE_LINK_STATIC_CXX_FLAGS ${TEMP_EXE_LINK_CXX_FLAGS})
ELSE($ENV{COMPILE_STATIC} MATCHES "1")
	MESSAGE(STATUS "Compile dynamic")
	SET(LIB_TYPE SHARED)

	INCLUDE(cmake_find_modules/Find_lua.cmake)
	IF(NOT LUA_FOUND)
		ADD_SUBDIRECTORY(lua lua)
	ENDIF(NOT LUA_FOUND)

	INCLUDE(cmake_find_modules/Find_tinyxml.cmake)
	IF(NOT TINYXML_FOUND)
		ADD_SUBDIRECTORY(tinyxml tinyxml)
	ENDIF(NOT TINYXML_FOUND)
ENDIF($ENV{COMPILE_STATIC} MATCHES "1")

#disable file/line/function inside logging
#ADD_DEFINITIONS(-DFILE_OVERRIDE)
#ADD_DEFINITIONS(-DLINE_OVERRIDE)
#ADD_DEFINITIONS(-DFUNC_OVERRIDE)
#ADD_DEFINITIONS(-DASSERT_OVERRIDE)

#fine tunning
ADD_DEFINITIONS(-DCreateRTCPPacket=CreateRTCPPacket_mystyle_only_once)
ADD_DEFINITIONS(-DFeedDataAudioMPEG4Generic=FeedDataAudioMPEG4Generic_one_by_one)
ADD_DEFINITIONS(-DHandleTSVideoData=HandleVideoData_version3)

ADD_DEFINITIONS(-DHAS_LUA)

#INCLUDE(cmake_find_modules/Find_v8.cmake)
#IF(V8_FOUND)
#    ADD_DEFINITIONS(-DHAS_V8)
#ENDIF(V8_FOUND)

#memory pool
#ADD_DEFINITIONS(-DUSE_MEM_POOL)
#ADD_DEFINITIONS(-DDEBUG_MEM_POOL)

SET(GENERIC_FLAGS "-Wall -Werror")

#add 64 bit support for 32 but platforms
ADD_DEFINITIONS(-D__USE_FILE_OFFSET64)
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
MESSAGE(STATUS "We have a ${CMAKE_SYSTEM} system")
IF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	SET(GENERIC_FLAGS "${GENERIC_FLAGS} -fPIC -fdata-sections -ffunction-sections")
	IF(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-D${SOCKREACTOR})
	ELSE(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-DNET_KQUEUE)
	ENDIF(DEFINED SOCKREACTOR)
	STRING(TOLOWER ${CMAKE_SYSTEM} TEMP_CMAKE_SYSTEM)
	STRING(REGEX MATCH "kfreebsd*" TEMP_CMAKE_SYSTEM_MATCH ${TEMP_CMAKE_SYSTEM})
	IF(TEMP_CMAKE_SYSTEM_MATCH MATCHES "kfreebsd")
		ADD_DEFINITIONS(-DDFREEBSD)
	ELSE(TEMP_CMAKE_SYSTEM_MATCH MATCHES "kfreebsd")
		ADD_DEFINITIONS(-DFREEBSD)
		INCLUDE(cmake_find_modules/Find_execinfo.cmake)
	ENDIF(TEMP_CMAKE_SYSTEM_MATCH MATCHES "kfreebsd")
	SET(CMAKE_EXE_LINK_STATIC_C_FLAGS "${CMAKE_EXE_LINK_STATIC_C_FLAGS} -Wl,--gc-sections")
	SET(CMAKE_EXE_LINK_STATIC_CXX_FLAGS "${CMAKE_EXE_LINK_STATIC_CXX_FLAGS} -Wl,--gc-sections")
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin")
	SET(GENERIC_FLAGS "${GENERIC_FLAGS} -mmacosx-version-min=10.5")
	ADD_DEFINITIONS(-DOSX)
	IF(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-D${SOCKREACTOR})
	ELSE(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-DNET_KQUEUE)
	ENDIF(DEFINED SOCKREACTOR)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux")
	SET(GENERIC_FLAGS "${GENERIC_FLAGS} -fPIC -fdata-sections -ffunction-sections")
	ADD_DEFINITIONS(-DLINUX)
	IF(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-D${SOCKREACTOR})
	ELSE(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-DNET_EPOLL)
	ENDIF(DEFINED SOCKREACTOR)
	SET(CMAKE_EXE_LINK_STATIC_C_FLAGS "${CMAKE_EXE_LINK_STATIC_C_FLAGS} -Wl,--gc-sections")
	SET(CMAKE_EXE_LINK_STATIC_CXX_FLAGS "${CMAKE_EXE_LINK_STATIC_CXX_FLAGS} -Wl,--gc-sections")
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "SunOS")
	SET(GENERIC_FLAGS "${GENERIC_FLAGS} -fPIC -fdata-sections -ffunction-sections")
	ADD_DEFINITIONS(-DSOLARIS)
	ADD_DEFINITIONS(-DDONT_DEFINE_HTONLL)
	ADD_DEFINITIONS(-DNET_SELECT)
	SET(SOCKET_LIB "-lsocket")
	SET(CMAKE_EXE_LINK_STATIC_C_FLAGS "${CMAKE_EXE_LINK_STATIC_C_FLAGS} -Wl,--gc-sections")
	SET(CMAKE_EXE_LINK_STATIC_CXX_FLAGS "${CMAKE_EXE_LINK_STATIC_CXX_FLAGS} -Wl,--gc-sections")
ELSE(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	MESSAGE(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} not supported yet")
ENDIF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")

#debug/release
GET_FILENAME_COMPONENT(ABSOLUTE_ROOT_PATH ${CRTMPSERVER_SOURCES_ROOT} ABSOLUTE)
STRING(LENGTH ${ABSOLUTE_ROOT_PATH} ABSOLUTE_ROOT_PATH_LENGTH)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GENERIC_FLAGS} -fno-rtti")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GENERIC_FLAGS}")
SET(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -DWITH_SANITY_INPUT_BUFFER -DVALIDATE_FROMAT_SPECIFIERS -Wformat=2")
SET(CMAKE_CXX_FLAGS_RELEASE "-g -O3 -fvisibility=hidden -fno-strict-aliasing -DSHORT_PATH_IN_LOGGER=${ABSOLUTE_ROOT_PATH_LENGTH}")
#SET(CMAKE_VERBOSE_MAKEFILE TRUE)

#generate defines.h
IF (EXISTS ${CRTMPSERVER_SOURCES_ROOT}/common/include/defines.h)
	MESSAGE(STATUS "Defines are in place")
ELSE (EXISTS ${CRTMPSERVER_SOURCES_ROOT}/common/include/defines.h)
	MESSAGE(STATUS "We need to rebuild the defines")
	EXECUTE_PROCESS(COMMAND lua constants.lua cpp ${CRTMPSERVER_SOURCES_ROOT}/common/include/defines.h WORKING_DIRECTORY ${CRTMPSERVER_CONSTANTS_ROOT})
ENDIF(EXISTS ${CRTMPSERVER_SOURCES_ROOT}/common/include/defines.h)


IF($ENV{COMPILE_STATIC} MATCHES "1")
    ADD_SUBDIRECTORY(lua lua)
    ADD_SUBDIRECTORY(tinyxml tinyxml)
ENDIF($ENV{COMPILE_STATIC} MATCHES "1")
ADD_SUBDIRECTORY(common common)
ADD_SUBDIRECTORY(thelib thelib)
ADD_SUBDIRECTORY(applications applications)
ADD_SUBDIRECTORY(crtmpserver crtmpserver)
ADD_SUBDIRECTORY(tests)
